package com.cloud.workTest;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import org.mozilla.universalchardet.UniversalDetector;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class CharsetTest {

    public static void main(String[] args) {
        String filePath = "C:\\Users\\86159\\Desktop\\A_10w-test.csv";

        try (FileInputStream fis = new FileInputStream(filePath)) {
            UniversalDetector detector = new UniversalDetector(null);

            byte[] buf = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buf)) > 0 && !detector.isDone()) {
                detector.handleData(buf, 0, bytesRead);
            }

            detector.dataEnd();
            String detectedCharset = detector.getDetectedCharset();

            if (detectedCharset != null) {
                System.out.println("Detected Charset: " + detectedCharset);
            } else {
                System.out.println("Charset detection failed.");
            }

            detector.reset();

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(isAscii(filePath));



    }


    private static boolean isAscii(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             InputStreamReader isr = new InputStreamReader(fis, "ASCII")) {

            boolean isAscii = true;
            int c;

            // 逐字符读取文件内容，并检查是否为 ASCII 字符
            while ((c = isr.read()) != -1) {
                if (c > 127) {
                    isAscii = false;
                    break;
                }
            }
            return isAscii;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
